//
//  moreImage.m
//  lyxluyou
//
//  Created by mmyy on 15/10/18.
//  Copyright (c) 2015年 mmyy. All rights reserved.
//

#import "moreImage.h"

#import "moreImageItems.h"

#import "lookImageVC.h"
@interface moreImage ()<UICollectionViewDataSource, UICollectionViewDelegate>

@end

@implementation moreImage
{
    NSMutableArray *_dataSoure;
    int currentCount;
    BOOL isUpdata;
    UICollectionView *_collection;
}
- (void)viewDidLoad {
    [super viewDidLoad];
    currentCount = 21;
    isUpdata = NO;
    self.automaticallyAdjustsScrollViewInsets = NO;
    self.navigationController.navigationBar.translucent= YES;
    self.view.backgroundColor = [UIColor whiteColor];
    [self loadData];
    [self createCollectionView];
    // Do any additional setup after loading the view.
}

-(void)loadData
{
    
    [Funcation getWithURL:[NSString stringWithFormat:moreImageUrl, self.vid, currentCount] success:^(id responseObject) {
        _dataSoure = [NSMutableArray arrayWithCapacity:0];
        NSArray *itmes = [responseObject objectForKey:@"items"];
        for (NSDictionary *dic in itmes) {
            moreImageItems *models = [moreImageItems moewImageItemsWithDict:dic];
            [_dataSoure addObject:models];
        }
        [_collection reloadData];
        if (isUpdata) {
            [_collection headerEndRefreshing];
            [_collection footerEndRefreshing];
        }
        
    } andFail:^(id error) {
        NSLog(@"---->>失败");
    }];

}
-(void)createCollectionView
{
    UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc] init];
    CGFloat appW = (self.view.frame.size.width - 15) / 3;
    layout.itemSize = CGSizeMake(appW - 5 , 150);
    layout.scrollDirection = UICollectionViewScrollDirectionVertical;
    _collection = [[UICollectionView alloc] initWithFrame:CGRectMake(0, 64, self.view.frame.size.width, self.view.frame.size.height - 64) collectionViewLayout:layout];
    _collection.delegate = self;
    _collection.dataSource = self;
    [_collection registerClass:[UICollectionViewCell class] forCellWithReuseIdentifier:@"basis-cell"];
    [self.view addSubview:_collection];
    __weak UICollectionView *newColl = _collection;
    __weak moreImage *isSelf = self;
    
    [_collection addHeaderWithCallback:^{
        newColl.headerRefreshingText = @"下拉刷新";
        [isSelf loadData];
        isUpdata = YES;
    }];
    [_collection addFooterWithCallback:^{
        currentCount += 21;
        newColl.footerRefreshingText = @"上拉刷新中";
        [isSelf loadData];
        isUpdata = YES;
    }];
    


}
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    return _dataSoure.count;
    
}
-(NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView
{
    return 1;
}

// The cell that is returned must be retrieved from a call to -dequeueReusableCellWithReuseIdentifier:forIndexPath:
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *ident = @"basis-cell";
    UICollectionViewCell *coll = [collectionView dequeueReusableCellWithReuseIdentifier:ident forIndexPath:indexPath];
    UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, (self.view.frame.size.width - 15) / 3, 150)];
    imageView.backgroundColor = [UIColor orangeColor];
    moreImageItems *images = [_dataSoure objectAtIndex:indexPath.row];
    [imageView setImageWithURL:[NSURL URLWithString:images.pohot] placeholderImage:[UIImage imageNamed:@"define"]];
    [coll.contentView addSubview:imageView];
    return coll;
    
}
-(void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
    lookImageVC *look = [[lookImageVC alloc] init];
    look.dataSource = _dataSoure;
    look.currentPice = indexPath.row;
    look.titleName = self.navigationItem.title;
    [self.navigationController pushViewController:look animated:YES];

}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
